package com.ultimatech.webapp.service.impl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ultimatech.webapp.*;
import com.ultimatech.webapp.service.IWebappService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangleping on 2017/11/10.
 */
@Service
public class WebappServiceImpl implements IWebappService {

    @Value("${token.url}")
    private String tokenUrl;

    @Value("${token.recCodeUrl}")
    private String recCodeUrl;

    public String getToke(String code) throws IOException {
        String url = this.tokenUrl;
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("code", code);
        param.put("grant_type", "authorization_code");
        param.put("redirect_uri", this.recCodeUrl);
        String json = HttpClientUtil.httpPostRequest(url, param);
        Gson gson = new GsonBuilder().create();
        AccessToken accessToken = gson.fromJson(json, AccessToken.class);
        return accessToken.getAccessToken();
    }

    @Value("${remote.url}")
    private String remoteUrl;

    private Gson gson = new GsonBuilder().create();

    public UserList findAllUser(String accessToken) throws URISyntaxException {
        String url = this.remoteUrl + "findAllUser?access_token=" + accessToken;
//        Map<String, Object> param = new HashMap<String, Object>();
//        param.put("access_token", accessToken);
        String json = HttpClientUtil.httpGetRequest(url);
        UserList message = gson.fromJson(json, UserList.class);
        return message;
    }

    public UserModel createUser(String accessToken, String name, String number) throws URISyntaxException {
        String url = this.remoteUrl + "createUser?access_token=" + accessToken + "&name=" + name + "&number=" + number;
        String json = HttpClientUtil.httpGetRequest(url);
        return gson.fromJson(json, UserModel.class);
    }

    public UserModel getUserById(String accessToken, Integer id) {
        String url = this.remoteUrl + "getuser?id=" + id + "&access_token=" + accessToken;
        String json = HttpClientUtil.httpGetRequest(url);
        return gson.fromJson(json, UserModel.class);
    }

    public UserModel updateUser(String accessToken, Integer id, String name, String number) throws URISyntaxException {
        String url = this.remoteUrl + "updateUser?access_token=" + accessToken + "&id=" + id + "&name=" + name + "&number=" + number;
        String json = HttpClientUtil.httpGetRequest(url);
        return gson.fromJson(json, UserModel.class);
    }

    public UserDelMsg delUser(String accessToken, Integer id) throws URISyntaxException {
        String url = this.remoteUrl + "delUser?userId=" + id + "&access_token=" + accessToken;
        String json = HttpClientUtil.httpGetRequest(url);
        return gson.fromJson(json, UserDelMsg.class);
    }
}
